#ifndef ACEMESH_UTILS
#define ACEMESH_UTILS

#include <stddef.h>
#include <time.h>

#include "task.h"
long long tick_count_now();

unsigned long rpcc();
unsigned long rtc();
// extern int total_num_threads;
// extern int cgid;
// #ifdef GS_AGENT
// #define thread_id 0
// #else
// #define thread_id total_num_threads-1
// #endif

/** Defined in tbb_main.cpp **/
unsigned GetPrime(unsigned seed);

#define mymax(a, b) ((a > b) ? (a) : (b))
#define mymin(a, b) ((a < b) ? (a) : (b))

//! A fast random number generator.
/** Uses linear congruential method. */
/*class FastRandom {
    unsigned x, a;
public:
    //! Get a random number.
    unsigned short get() {
        return get(x);
    }
    //! Get a random number for the given seed; update the seed for next use.
    unsigned short get( unsigned& seed ) {
        unsigned short r = (unsigned short)(seed>>16);
        seed = seed*a+1;
        return r;
    }
    //! Construct a random number generator.
    FastRandom( unsigned seed ) {
        x = seed;
        a = GetPrime( seed );
    }
};*/
//}

#ifndef DISTRIBUTED_SCHEDULER
#define MS_SCHED 1
#define SL_SCHED 0
#endif

#ifdef ACEMESH_GRAPH_BUILD_PROFILING
#define N_BUILD_PROF 44
#define T_GRAPH_BUILD 0  //
#define T_GRAPH_EXEC 1   //
#define T_GRAPH_TIME 2   //
#define BEGIN_SPLIT 3
#define ARGS_TRANS 4
#define TASK_CONSTR 5  //
#define TASK_GENE 6    //
#define FAT_TASK_GEN  7
#define REG_TASK_IN 8  //
#define END_TASKS_L 9
#define BUILD_RELAT 10
#define BUILD_RELAT_IN 11
#define ADD_SUCC 12
#define DEL_END_L 13
#define IF_CON 14
#define FIND_HASH 15
#define INSERT_HASH 16
#define TRAVE_LIST 17
#define NEED_SPAWN_P 18
#define VEC_CLEAR 19
#define END_SPLIT 20
#define UPDATE_HASH 21     //
#define UPDATE_ITEM_IN 22  //
#define FIND_HASH2 23      //
#define UPDATE_DATA 24     //
#define CLEAR_NEED_SPAWN 25
#define GET_END_LIST 26
#define JOIN_TASK 27    //
#define RESET_GRAPH 28  //
#define RESTROE_GRAPH 29
#define BUILD_TASK        30 //嵌套图等于T_GRAPH_BUILD
#define BUILD_SUBTASK     31 //构子图的时间
#define EXEC_SUBTASK      32 //子任务执行的时间
#define SUB_ARGS_TRANS    33  //调用acemesh_do_push()接口的时间
#define SUB_TASK_CONSTR   34 //acemesh_task_generator中new出一个任务的时间
#define SUB_TASK_GENE     35 //调用_task_gen的时间
#define SUB_FAT_TASK_GEN  36
#define SUB_REG_TASK_IN   37
#define SUB_VEC_CLEAR     38
#define SUB_BUILD_RELA    39
#define PACK_MESSAGE      40
#define SUB_PUSH_SPAWN    41
#define SUB_ADD_SUCC      42
#define FIND_PARENT       43
#endif
#define MULTI_TASK 512
#define BLOCK_SIZE 1024
#define MULTI_STATUS 16
#ifdef __cplusplus
extern "C" {
#ifdef TARG_SW9
void my_atomic_add(volatile int *ref);
#ifndef MULTI_SPAWN
void my_spawn_to_id(struct task *t);
#else
void my_spawn_to_id(int num);
#endif
void my_atomic_sub(volatile int *ref);
void my_add_successor(struct aceMesh_task *preTask);
void my_add_end_successor(struct aceMesh_task *preTask);
// void my_send_to_agent(struct aceMesh_task *dest, volatile int num);
#endif
#endif
void my_mutex_lock(volatile int *my_lock);
void my_mutex_unlock(volatile int *my_lock);
#ifdef __cplusplus
}
#endif

void slave_my_mutex_lock(volatile int *my_lock);
void slave_my_mutex_unlock(volatile int *my_lock);

#endif
